package com.lzw.blog.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzw.blog.admin.mapper.PermissionMapper;
import com.lzw.blog.admin.mapper.RoleMapper;
import com.lzw.blog.admin.mapper.RolePermissionMapper;
import com.lzw.blog.admin.model.params.PageParam;
import com.lzw.blog.admin.pojo.Category;
import com.lzw.blog.admin.pojo.Permission;
import com.lzw.blog.admin.pojo.Role;
import com.lzw.blog.admin.pojo.RolePermission;
import com.lzw.blog.admin.vo.PageResult;
import com.lzw.blog.admin.vo.Result;
import com.lzw.blog.admin.vo.RoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private  PermissionService permissionService;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;


    public Result listRole(PageParam pageParam) {
        Page<Role> page = new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize());
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(pageParam.getQueryString())) {
            queryWrapper.like(Role::getName, pageParam.getQueryString());
        }
        Page<Role> rolePage = roleMapper.selectPage(page, queryWrapper);

        PageResult<RoleVo> pageResult = new PageResult<>();
        pageResult.setList(copyList(rolePage.getRecords()));
        pageResult.setTotal(rolePage.getTotal());
        return Result.success(pageResult);
    }

    public List<RoleVo> copyList(List<Role> t) {
        List<RoleVo> roleVoList = new ArrayList<>();
        for (Role role : t) {
            RoleVo roleVo = new RoleVo();
            roleVo.setId(role.getId());
            roleVo.setName(role.getName());
            roleVo.setPermissions(permissionService.findPermissionsByRoleId(role.getId()));
            roleVoList.add(roleVo);
        }
        return roleVoList;
    }

    public Result deletePermissionByRoleId(Long roleId, Long permissionId) {
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId, roleId)
                .eq(RolePermission::getPermissionId, permissionId);
        int deletedCount = rolePermissionMapper.delete(queryWrapper);
        if (deletedCount > 0) {
            return Result.success("删除成功");
        } else {
            return Result.fail(400, "删除失败");
        }
    }

    public Result setPermissionForRole(Long roleId, Long permissionId) {
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId, roleId)
                .eq(RolePermission::getPermissionId, permissionId);
        int selectCount = rolePermissionMapper.selectCount(queryWrapper);
        if (selectCount > 0) {
            return Result.success("授权成功");
        }
        RolePermission rolePermission = new RolePermission();
        rolePermission.setRoleId(roleId);
        rolePermission.setPermissionId(permissionId);
        int insertCount = rolePermissionMapper.insert(rolePermission);
        if (insertCount > 0) {
            return Result.success("授权成功");
        }else {
            return Result.fail(400, "授权失败");
        }

    }

    public Result add(Role role) {
        int insertCount = roleMapper.insert(role);
        if (insertCount > 0) {
            return Result.success("新增成功");
        }else {
            return Result.fail(500, "系统异常");
        }
    }

    public Result update(Role role) {
        int updateCount = roleMapper.updateById(role);
        if (updateCount > 0) {
            return Result.success("修改成功");
        }else {
            return Result.fail(500, "系统异常");
        }
    }

    public Result delete(Long id) {
        LambdaQueryWrapper<RolePermission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RolePermission::getRoleId, id);
        rolePermissionMapper.delete(lambdaQueryWrapper);
        int deleteCount = roleMapper.deleteById(id);
        if (deleteCount > 0) {
            return Result.success("删除成功");
        }else {
            return Result.fail(500, "系统异常");
        }
    }

    public Role findRoleById(Long id) {
        return roleMapper.selectById(id);
    }

    public Result getAllRole() {
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return Result.success(roleMapper.selectList(lambdaQueryWrapper));
    }
}
